package com.jyy.prefabricated.utils;


import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.entity.TrainingOrganization;
import com.jyy.prefabricated.enums.UserTypeEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.jwt.JwtTokenUtil;
import com.jyy.prefabricated.mapper.StudentMapper;
import com.jyy.prefabricated.mapper.SysUserMapper;
import com.jyy.prefabricated.mapper.TrainingOrganizationMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class UserUtils {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TrainingOrganizationMapper trainingOrganizationMapper;

    @Autowired
    private StudentMapper studentMapper;

    public SysUser getCurrentUser(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            String username = jwtTokenUtil.getUsernameFromToken(token);
            SysUser user = sysUserMapper.selectByUsernameOrIdCard(username);
            if(user != null) {
                log.info("用户类型A"+user.getUserType());
                return user;
            }        }
        throw new CustomException(401, "请登录后再试");
    }


    public String getCurrentUsername(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            return jwtTokenUtil.getUsernameFromToken(token);
        }
        throw new CustomException(401, "请登录后再试");
    }

    public String getCurrentUsernameByCode(String token){
        if (StringUtils.isNotBlank(token) ) {
            return jwtTokenUtil.getUsernameFromToken(token);
        }
        return null;
        //throw new CustomException(401, "请登录后再试");
    }

    public TrainingOrganization getCurrentTrainingOrganization(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            String account = jwtTokenUtil.getUsernameFromToken(token);
            TrainingOrganization trainingOrganization = trainingOrganizationMapper.selectByAccount(account);
            if(trainingOrganization != null) {
                return trainingOrganization;
            }
        }
        throw new CustomException(401, "请登录后再试");
    }

    public Student getCurrentStudent(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            String account = jwtTokenUtil.getUsernameFromToken(token);
            Student student = studentMapper.selectByTel(account);
            if(student != null) {
                return student;
            }
        }
        throw new CustomException(401, "请登录后再试");
    }


    /**
     * 获取当前登录用户名
     * @return
     */
    public String getCurrentAccount(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            String username = jwtTokenUtil.getUsernameFromToken(token);
            return username;
        }
        throw new CustomException(401, "请登录后再试");
    }

    /**
     * 获取当前登录用户类型
     * @return
     */
    public int getUserType(){
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token) ) {
            String username = jwtTokenUtil.getUsernameFromToken(token);
            TrainingOrganization trainingOrganization = trainingOrganizationMapper.selectByAccount(username);
            if(trainingOrganization != null){
                return UserTypeEnum.TRAININGORGANIZATION.getValue();
            }
            Student student = studentMapper.selectByTel(username);
            if(student != null){
                return UserTypeEnum.STUDENT.getValue();
            }
            return UserTypeEnum.ADMIN.getValue();
        }
        throw new CustomException(401, "请登录后再试");
    }

}
